package yjpay.api.web.control;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import yjpay.service.PayAPIService;

/**
 * 绑卡支付示例
 * 
 * @author junqinghuang
 * 
 */
@Controller
public class BindCardPayWebCtrl {
	@Resource(name = "payAPIService")
	private PayAPIService payAPIService;

	@RequestMapping(value = "/pay/to_bindpay")
	public ModelAndView toBindPay(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		String defalut_orderid = "12345" + Math.random() * 30;
		String defalut_identityid = "user_" + Math.random() * 99900;
		modelAndView.addObject("default_orderid", defalut_orderid);
		modelAndView.addObject("defalut_identityid", defalut_identityid);
		modelAndView.addObject("default_ip", this.getRemoteIP(request));
		modelAndView.setViewName("pay/bind_pay.jsp");
		return modelAndView;
	}
	@RequestMapping(value = "/pay/to_sendvalidecode")
	public ModelAndView toSendvalideCode(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("pay/send_validate_code.jsp");
		return modelAndView;
	}
	@RequestMapping(value = "/pay/to_confirmpay")
	public ModelAndView toConfirmPay(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("pay/confirm_pay.jsp");
		return modelAndView;
	}

	@RequestMapping(value = "/pay/bindpay")
	public ModelAndView bindpayRequest(Model model,
			@RequestParam("pay_amount") int amount,
			@RequestParam("pay_bindid") String bindid,
			@RequestParam("pay_orderid") String orderid,
			@RequestParam("pay_identityid") String identityid,
			@RequestParam("pay_identitytype") int identitytype,
			@RequestParam("pay_userip") String userip,
			@RequestParam("pay_other") String other,
			@RequestParam("pay_productcatalog") String productcatalog,
			@RequestParam("pay_productname") String productname,
			@RequestParam("pay_productdesc") String productdesc,
			@RequestParam("pay_callbackurl") String callbackurl,
			@RequestParam("pay_fcallbackurl") String fcallbackurl,
			@RequestParam("pay_terminaltype") int terminaltype,
			@RequestParam("pay_terminalid") String terminalid,
			HttpServletRequest request) throws Exception {

		int currency = 156;
		long i = System.currentTimeMillis() / 1000;
		System.out.println("transtime:" + i);
		Integer transtime = (int) i;

		String payresult = payAPIService.bindCardPayRequest(amount, bindid,
				currency, identityid, identitytype, orderid, other,
				productcatalog, productdesc, productname, transtime, userip,
				callbackurl, fcallbackurl, terminaltype, terminalid);

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("pay/pay_result.jsp");
		modelAndView.addObject("apiname", "绑卡支付请求接口");
		modelAndView.addObject("payresult_view", payresult);

		return modelAndView;
	}
	
	@RequestMapping(value = "/pay/sendvalidecode")
	public ModelAndView sendvalideCode(Model model,
			@RequestParam("orderid") String  orderid,
			HttpServletRequest request) throws Exception {

		String payresult = payAPIService.sendvalideCode(orderid);

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("pay/pay_result.jsp");
		modelAndView.addObject("apiname", "发送短信验证码接口");
		modelAndView.addObject("payresult_view", payresult);

		return modelAndView;
	}
	
	
	@RequestMapping(value = "/pay/confirmpay")
	public ModelAndView comfirmPay(Model model,
			@RequestParam("orderid") String  orderid,
			@RequestParam("validecode") String  validecode,
			HttpServletRequest request) throws Exception {

		String payresult = payAPIService.confirmPay(orderid, validecode);

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("pay/pay_result.jsp");
		modelAndView.addObject("apiname", "确认支付接口");
		modelAndView.addObject("payresult_view", payresult);

		return modelAndView;
	}
	

	public String getRemoteIP(HttpServletRequest request) {
		if (request.getHeader("x-forwarded-for") == null) {
			return request.getRemoteAddr();
		}
		return request.getHeader("x-forwarded-for");
	}
}
